<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2018/2/3
 * Time: 15:11
 */

namespace app\common\controller;


use think\Request;

class BaseConfig
{
    public $path='';

    public function __construct()
    {
        $module=Request::instance()->module();
        $this->path=ROOT_PATH.DS.'config'.DS.$module.DS.'config.json';
        if(!file_exists($this->path)) fopen($this->path,'w');
    }

    public static function instance()
    {
        return new static();
    }

    public function getConfig()
    {
        return json_decode(file_get_contents($this->path),true);
    }

    public function setConfig(array $data)
    {
        $config=$this->getConfig();
        foreach ($data as $key=>$val) {
            if (isset($config[$key])){
                $config[$key]=$val;
            }
        }
        file_put_contents($this->path,json_encode($config));
        return 1;
    }
}